package com.example.transit.utils;

import android.util.Log;

import com.example.transit.bean.ServerLineInfo;
import com.example.transit.bean.StationInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ProjectDataUtils {

    List<StationInfo> allStationlist = new ArrayList<>();//车站信息列表
    List<StationInfo> stationlist = new ArrayList<>();//车站信息列表
    List<StationInfo> upstationlist = new ArrayList<>();
    List<StationInfo> choiceStationlist=new ArrayList<>();
    List<StationInfo> downstationlist = new ArrayList<>();
    List<ServerLineInfo> serverLinelist=new ArrayList<>();
    Boolean isupline = true;//是否是上行；
    //解析所有站点名称信息
    //解析服务端传送的线路名称信息
    public  List<StationInfo> analyseAllStationName(String resStr) {
        allStationlist.clear();
        List<StationInfo> listItem=new ArrayList<>();
        resStr = resStr.trim();
        String[] data = resStr.split("\n");
        for (int i = 0; i < data.length; i++) {
            String datainfo = data[i].trim();
            String[] oneData=datainfo.split("\\^#");
           // String[][] dataitems=null;
            StationInfo info = new StationInfo();
            info.setLineNum(oneData[0]);
            info.setStationName(oneData[9]);
            info.setLinelongitude(Float.parseFloat(oneData[3]));
            info.setLinelatitude(Float.parseFloat(oneData[4]));
            info.setIsRealStation(oneData[8]);
            info.setNext_distance(Float.parseFloat(oneData[11]));
            if (oneData[5].equals("1")){
                info.setIsupline(true);
            }else {
                info.setIsupline(false);
            }
            info.setLineStationOrder(oneData[6]);
            allStationlist.add(info);
        }
        return allStationlist;
    }
    //解析筛选等等线路名称信息
    public  List<StationInfo> analyseChoiceStationName(String resStr,String choicestr) {
        choiceStationlist.clear();
        resStr = resStr.trim();
        String[] data = resStr.split("\n");
        for (int i = 0; i < data.length; i++) {
            String datainfo = data[i].trim();
            String[] oneData=datainfo.split("\\^#");
            // String[][] dataitems=null;
            StationInfo info = new StationInfo();
            info.setLineNum(oneData[0]);
            info.setStationName(oneData[9]);
            info.setLinelongitude(Float.parseFloat(oneData[3]));
            info.setLinelatitude(Float.parseFloat(oneData[4]));
            info.setIsRealStation(oneData[8]);
            info.setNext_distance(Float.parseFloat(oneData[11]));
            if (oneData[5].equals("1")){
                info.setIsupline(true);
            }else {
                info.setIsupline(false);
            }
            info.setLineStationOrder(oneData[6]);
            if (oneData[0].equals(choicestr)) {
                choiceStationlist.add(info);
            }
        }
        return choiceStationlist;
    }

    //获取所有线路集合
    public  List<List<StationInfo>> getAllChoiceStList(String resStr,String choicestr) {
        List<StationInfo> upchoicestationlist = new ArrayList<>();
        List<StationInfo> downchoiceStationlist=new ArrayList<>();
        List<List<StationInfo>> getAllChoiceStList=new ArrayList<>();
        upchoicestationlist.clear();
        downchoiceStationlist.clear();
        getAllChoiceStList.clear();
        resStr = resStr.trim();
        String[] data = resStr.split("\n");
        for (int i = 0; i < data.length; i++) {
            String datainfo = data[i].trim();
            String[] oneData=datainfo.split("\\^#");
            // String[][] dataitems=null;
            StationInfo info = new StationInfo();
            info.setLineNum(oneData[0]);
            info.setStationName(oneData[9]);
            info.setLinelongitude(Float.parseFloat(oneData[3]));
            info.setLinelatitude(Float.parseFloat(oneData[4]));
            info.setIsRealStation(oneData[8]);
            info.setNext_distance(Float.parseFloat(oneData[11]));
            if (oneData[5].equals("1")){
                info.setIsupline(true);
            }else {
                info.setIsupline(false);
            }
            info.setLineStationOrder(oneData[6]);
            if (oneData[0].equals(choicestr)&&oneData[5].equals("1")) {
                upchoicestationlist.add(info);

            }else if (oneData[0].equals(choicestr)&&oneData[5].equals("0")){
                downchoiceStationlist.add(info);
            }
        }
      //  if (upchoicestationlist!=null&&upchoicestationlist.size()!=0&&downchoiceStationlist!=null&&downchoiceStationlist.size()!=0) {
            getAllChoiceStList.add(upchoicestationlist);
            getAllChoiceStList.add(downchoiceStationlist);
      // }
        return getAllChoiceStList;
    }
    //获取所有线路集合
    public  List<ServerLineInfo> getAllLineNum(String resStr) {
        serverLinelist.clear();
        resStr = resStr.trim();
        String[] data = resStr.split("<br/>");
        for (int i=0;i<data.length;i++){
            ServerLineInfo serverLineInfo=new ServerLineInfo();
            serverLineInfo.setLineNum(data[i]);
            serverLinelist.add(serverLineInfo);
        }
        return serverLinelist;
    }

    //获取数据端站点信息数据方法
    public String httpGet(String server, String path, String params, String cookie) {
        String jsons = null;
        String httpUrl = server + path + "?" + params;
        //       System.out.println("****httpGet = " + httpUrl + "　param = " + params);
//		 System.out.println(httpUrl);
        URL url;
        try {
            url = new URL(httpUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setRequestMethod("GET");
            if (cookie != null)
                conn.setRequestProperty("Cookie", cookie);
            conn.setRequestProperty("Content-Language", "zh-cn");
            conn.setRequestProperty("Connection", "keep-alive");
            InputStream input = conn.getInputStream();
            InputStreamReader inputreader = new InputStreamReader(input);
            BufferedReader reader = new BufferedReader(inputreader);
            String str = null;
            StringBuffer sb = new StringBuffer();
            while ((str = reader.readLine()) != null) {
                sb.append(str).append("\n");
            }
            if (sb != null) {
                jsons = sb.toString();
            }
            reader.close();
            inputreader.close();
            input.close();
            conn.disconnect();
            input = null;
            inputreader = null;
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            Log.e("busappmas", "http get error URLException");
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("busappmas", "http get error IOException");
            return null;
        } catch (Exception e) {
            Log.e("busappmas", "http get error Exception");
            return null;
        }
        return jsons;
    }

}
